// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: 'cloud-demo-dev-4gvtj96e9f7324c6'
});

const db = cloud.database();
const collection = db.collection('comments');

// 云函数入口函数
exports.main = async (event, context) => {

  const {
    OPENID,
    APPID,
    UNIONID
  } = cloud.getWXContext();
  const _ = db.command;

  let res = await db.collection('comments').add({
    data: {
      _openid: OPENID,   //发表评论用户的openid
      id: event.id,  //笑话的id
      content: event.content,   //评论的内容 
      like_count:event.like_count,   //评论点赞的条数
      created_at:event.created_at,  //评论的发表时间
      user:event.user
    }
  })

  return res;
}